/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package backend.service;

import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

/**
 * class for validating 
 *
 * @author Markus Moormann
 */
public class ValidationService {
	/**
	 * a logger to log errors, debug information, etc.
	 */
	private static Logger LOG = Logger.getLogger(ValidationService.class);
	/**
	 * possible mime type for powerpoint
	 */
	private static String[] mimeTypes = { "application/x-mspowerpoint",
										  "application/mspowerpoint", 
										  "application/powerpoint",
										  "application/vndms-powerpoint", 
										  "application/vnd.ms-powerpoint",
										  "application/msword",
										  "application/pdf"};

	/**
	 * checks whether the given URL delivers a correct powerpoint file
	 * 
	 * @param url the URL to check
	 * @return true when it is a ppt, false else
	 */
	public boolean checkForPPTOnServer(String url) {
		boolean result = false;
		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod(url);
		try {
			client.executeMethod(method);
			Header contentHeader = method.getResponseHeader("Content-type");
			LOG.debug(contentHeader.getValue());
			for (int i = 0; i < mimeTypes.length; i++) {
				if (mimeTypes[i].equals(contentHeader.getValue())) {
					result = true;
				}
			}
		} catch (HttpException e) {
			LOG.error(e);
			e.printStackTrace();
		} catch (IOException e) {
			LOG.error(e);
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * checks whether the given URL points to a valid converted slide folder
	 * 
	 * @param url the URL to check
	 * @return true if URL points to correct folder, false else
	 */
	public boolean checkConvertedSlideForCorrectnessCommand(String url) {
		boolean result = false;

		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod(url);
		try {
			if (client.executeMethod(method) == HttpStatus.SC_OK) {
				result = true;
			}
		} catch (HttpException e) {
			LOG.error(e);
			e.printStackTrace();
		} catch (IOException e) {
			LOG.error(e);
			e.printStackTrace();
		} 
		return result;
	}

}
